package com.reshare.controller;

import com.reshare.base.BaseController;
import com.reshare.base.ResultInfo;
import com.reshare.bean.Role;
import com.reshare.query.RoleQuery;
import com.reshare.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    //访问角色主页面
    @RequestMapping("index")
    public String index() {
        return "role/role";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> list(RoleQuery query){
        return roleService.SelectRoleList(query);
    }

    @RequestMapping("addOrUpdateRole")
    public String addOrUpdate(Integer roleId, Model model) {
        System.out.println(roleId);
        //判断
        if (roleId != null) {
            //存储
            model.addAttribute("role", roleService.selectByPrimaryKey(roleId));
        }
        return "role/add_update";
    }

    //添加
    @RequestMapping("save")
    @ResponseBody
    public ResultInfo save(Role role) {
        System.out.println(role);
        roleService.saveRole(role);
        return success("添加成功");
    }

    //更新
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo update(Role role) {
        System.out.println(role + "1111111");
        roleService.update(role);
        return success("修改成功");
    }

    //打开授权界面
    @RequestMapping("toAddGrantPage")
    public String sayGrand(Integer roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return "role/grant";
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo delete(Integer id) {
        System.out.println(id + "1111111");
        roleService.deleteRole(id);
        return success("删除成功");
    }


    @RequestMapping("addGrant")
    @ResponseBody
    public ResultInfo grant(Integer roleId,String [] mids){
        //调用
        System.out.println(roleId+"-----"+mids);
        roleService.addGrant(roleId,mids);
        //判断是否成功
        return success("角色授权成功");
    }

    @RequestMapping("roles")
    @ResponseBody
    public List<Map<String,Object>> findRoles(Integer userId){

        List<Map<String, Object>> roles = roleService.findRoles(userId);
        return roles;
    }



}
